/**
 *  quotemaster.vala
 *
 *  Copyright (c) 2007 Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

// valac --pkg sqlite3 --pkg gtk+-2.0 --pkg libglade-2.0 -X "-Wl,--export-dynamic" -X -rdynamic quotemaster.vala -o quotemaster

using GLib;
using Gtk;
using Glade;
using Sqlite;

private enum QuotesFields {
    ID,
    TEXT,
    AUTHOR,
    TAGS
}

public class Quotemaster : GLib.Object
{

    [Widget] private static ComboBox author_combo;
    [Widget] private static ListStore author_list;
    [Widget] private static TreeView quotes_treeview;
    [Widget] private static ListStore quotes_store;
    [Widget] private static TreeModelFilter quotes_filter;
    [Widget] private static Entry text_search_entry;

    [Widget] private static Dialog add_quote_dialog;
    [Widget] private static TextView quote_textview;
    [Widget] private static TextBuffer quote_textbuffer;
    [Widget] private static TextView tags_textview;
    [Widget] private static TextBuffer tags_textbuffer;
    [Widget] private static Entry author_entry;
    [Widget] private static Entry tags_entry;

    [Widget] private static AboutDialog about;

    public Sqlite.Database quotes_db;
    private static Glade.XML gxml;

    construct {
        gxml = new Glade.XML ("qmwindow.glade", null, null);

        // Widgets
        author_combo = (ComboBox) gxml.get_widget("author_combo");
        tags_textview = (TextView) gxml.get_widget("tags_textview");
        quotes_treeview = (TreeView) gxml.get_widget("quotes_treeview");
        text_search_entry = (Entry) gxml.get_widget("text_search_entry");

        add_quote_dialog = (Dialog) gxml.get_widget("add_quote_dialog");
        author_entry = (Entry) gxml.get_widget("author_entry");
        tags_entry = (Entry) gxml.get_widget("tags_entry");
        quote_textview = (TextView) gxml.get_widget("quote_textview");
        quote_textbuffer = new TextBuffer(null);
        quote_textview.set_buffer(quote_textbuffer);

        about = (AboutDialog) gxml.get_widget("about_dialog");

        // Connect signals
        //gxml.signal_autoconnect();
        gxml.signal_connect("on_qmwindow_delete_event", on_qmwindow_delete_event);
        gxml.signal_connect("on_quit_activate", on_quit_activate);
        gxml.signal_connect("on_new_activate", on_new_activate);
        gxml.signal_connect("on_copy_activate", on_copy_activate);
        gxml.signal_connect("on_delete_activate", on_delete_activate);
        gxml.signal_connect("on_about_activate", on_about_activate);
        gxml.signal_connect("on_add_quote_button_clicked", on_add_quote_button_clicked);
        gxml.signal_connect("on_remove_quote_button_clicked", on_remove_quote_button_clicked);
        gxml.signal_connect("on_about_dialog_delete_event", on_about_dialog_delete_event);
        gxml.signal_connect("on_about_dialog_close", on_about_dialog_close);
        gxml.signal_connect("on_about_dialog_response", on_about_dialog_response);
        gxml.signal_connect("on_add_quote_dialog_response", on_add_quote_dialog_response);

        // Initialize data models
        init_db("quotes.db");
        init_author_combo();
        init_quotes_list();
        init_tags_textview();


    }

    private void init_author_combo() {
        author_list = new ListStore(1, typeof(string));

        var cell = new CellRendererText();
        author_combo.pack_start(cell, true);

        TreeIter iter;
        author_list.append(ref iter);
        author_list.set(ref iter, 0, "Todos");

        author_combo.set_model(author_list);
        author_combo.set_active(0);
    }

    private void init_quotes_list() {
        quotes_store = new ListStore(4, typeof(int), typeof(string),
                                        typeof(string), typeof(string));

        TreeViewColumn column1 = new TreeViewColumn.with_attributes("Citacao",
                                     new CellRendererText(),
                                     "text", QuotesFields.TEXT);
        column1.min_width = 400;
        TreeViewColumn column2 = new TreeViewColumn.with_attributes("Autor",
                                     new CellRendererText(),
                                     "text", QuotesFields.AUTHOR);

        quotes_treeview.append_column(column1);
        quotes_treeview.append_column(column2);

        quotes_filter = (TreeModelFilter) new TreeModelFilter(quotes_store, null);
        quotes_treeview.set_model(quotes_filter);
        quotes_filter.set_visible_func((TreeModelFilterVisibleFunc) visible_quotes, null, null);

        //self.quoteFilter = self.quoteStore.filter_new()
        //self.quoteTreeview.set_model(self.quoteFilter)
        //self.quoteFilter.set_visible_func(self.quotesVisible, data=None)
    }

    private void init_tags_textview() {
    }

    // Callbacks
    static bool on_qmwindow_delete_event(Window window, Gdk.Event event) {
        Gtk.main_quit();
    }

    static void on_quit_activate(Widget widget) {
        on_qmwindow_delete_event(null, null);
    }

    static void on_new_activate(Widget widget) {
        new_quote();
    }

    static void on_copy_activate(Widget widget) {
    }

    static void on_delete_activate(Widget widget) {
    }

    static void on_remove_quote_button_clicked(Widget widget) {
    }

    static void on_autor_combo_changed(Widget widget) {
    }

    static void on_text_search_entry_changed(Widget widget) {
    }

    // About Dialog
    static void on_about_activate(Widget widget) {
        about.show();
    }
    static void hide_about() {
        about.hide();
    }
    static void on_about_dialog_delete_event(Widget widget) {
        hide_about();
    }
    static void on_about_dialog_close(Widget widget) {
        hide_about();
    }
    static void on_about_dialog_response(Widget widget) {
        hide_about();
    }

    // Add Quote Dialog
    static void on_add_quote_button_clicked(Widget widget) {
        new_quote();
    }
    static void on_add_quote_dialog_delete_event(Widget widget, Gdk.Event event) {
        add_quote_dialog.hide();
    }
    static void on_add_quote_dialog_response(Widget widget, int response_id) {
        if (response_id == ResponseType.CANCEL) {
            add_quote_dialog.hide();
        } else if (response_id == ResponseType.OK) {
            TextIter start;
            TextIter end;
            quote_textbuffer.get_start_iter(ref start);
            quote_textbuffer.get_end_iter(ref end);

            string txt = quote_textbuffer.get_text(ref start, ref end, false);
            string author = author_entry.get_text();
            string tags = tags_entry.get_text();

            TreeIter iter;
            quotes_store.append(ref iter);
            quotes_store.set(ref iter, QuotesFields.ID, 0,
                                   QuotesFields.TEXT, txt,
                                   QuotesFields.AUTHOR, author,
                                   QuotesFields.TAGS, tags);
        }
    }

    // Methods
    public void init_db(string db_file) {
        string query = "SELECT * FROM quotes ORDER BY text";
        int v = Database.open(db_file, ref quotes_db);
        stdout.printf("v: %d\n", v);
        v = quotes_db.exec (query);
        stdout.printf("v: %d\n", v);
    }

    static void new_quote() {
        quote_textbuffer.set_text("", 0);
        author_entry.set_text("");
        tags_entry.set_text("");
        //add_quote_dialog.run();
        add_quote_dialog.show();
    }

    private bool visible_quotes(TreeModel model, out TreeIter iter, pointer data) {
        string author, result;

        author = author_combo.get_active_text();

        if (author == "Todos")
            return true;
        else {
            model.get(ref iter, QuotesFields.AUTHOR, ref result);
            return (author == result);
        }
    }

    // Main
    static int main (string[] args) {
        Gtk.init(out args);
        Quotemaster app = new Quotemaster();
        Gtk.main();
        return 0;
    }
}
